<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img src="../assets/薇拉.png"
             alt="战双帕弥什-薇拉"
             id="obj-img">
        <span>电商后台管理系统</span>
      </div>

      <el-button type="info"
                 @click="logout">
        退出
      </el-button>
    </el-header>
    <!-- 页面主体区域 -->
    <el-container>
      <!-- 侧边栏区域 -->
      <el-aside :width="isCollapse?'64px':'200px'">
        <div class="toggle-button"
             @click="toggleCollapse">|||</div>
        <!-- 侧边栏菜单区域 -->
        <el-menu background-color="#333744"
                 text-color="#fff"
                 active-text-color="#409EFF"
                 unique-opened
                 :collapse="isCollapse"
                 :collapse-transition="false"
                 router
                 :default-active="activePath">
          <!-- unique-opened设置唯一打开一级菜单 -->
          <!-- collapse折叠 -->
          <!-- collapse-transition折叠动画 -->
          <!-- 一级菜单 -->
          <!-- :index="item.id+''"为每一个一级菜单指定唯一的下拉事件下标，需要的参数为字符串 -->
          <!-- 开启为侧边栏menu开启路由模式 -->
          <el-submenu :index="item.id+''"
                      v-for="(item,i) in menuList"
                      :key="item.id">
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconsObj[i]"></i>
              <!-- 文本 -->
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item :index="'/'+subItem.path"
                          v-for="subItem in item.children"
                          :key="subItem.id"
                          @click="saveNavState('/'+subItem.path)">
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 文本 -->
                <span>{{subItem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧内容 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data () {
    return {
      menuList: [],
      iconsObj: ['iconfont icon-user', 'iconfont icon-tijikongjian', 'iconfont icon-shangpin', 'iconfont icon-danju', 'iconfont icon-baobiao'],
      isCollapse: false,
      // 被激活的链接地址
      activePath: ''
    };
  },
  created () {
    this.getMenuList()
    // 页面刷新是不会丢失当前高亮的操作菜单列表二级列表选择项
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    // 退出登录
    logout () {
      window.sessionStorage.clear();
      this.$router.push('/login');
    },
    // 获取操作菜单列表数据
    async getMenuList () {
      const { data: result } = await this.$http.get('menus')
      if (result.meta.status !== 200) return this.$message.error(result.meta.msg)
      this.menuList = result.data
    },
    // 点击按钮，切换菜单的折叠与展开
    toggleCollapse () {
      this.isCollapse = !this.isCollapse
    },
    // 保存操作菜单列表连接地址
    saveNavState (activePath) {
      window.sessionStorage.setItem('activePath', activePath)
    }
  }
};
</script>

<style lang='less' scoped>
.home-container {
  height: 100%;
}

.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center; //取消按钮上下贴边
  color: #fff;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    span {
      padding: 15px;
    }
    #obj-img {
      height: 65px;
      width: 65px;
      border-radius: 50%;
    }
  }
}

.el-aside {
  background-color: #333744;
  .el-menu {
    //解决二级列表凸出menu
    border-right: none;
  }
}

.el-main {
  background-color: #eaedf1;
}

.iconfont {
  margin-right: 10px;
}

.toggle-button {
  background-color: #4a5064;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>
